"""
    Main page handler
    Fixes some parameters for the GWT based GUI
    
    @author: Jean-Lou Dupont
    
    @deprecated
"""

import os
import sys
import logging
import string

import wsgiref.handlers
from google.appengine.ext.webapp import template 
from google.appengine.ext import webapp
from google.appengine.api import users

#from libs.wiki import template

#make sure we initialize django properly... DO NOT TOUCH!!!
setattr(template.django.conf.settings, 'TEMPLATE_LOADERS', 
                ('libs.wiki.db_template_loader.load_template_source',
                 'django.template.loaders.filesystem.load_template_source' ))

class MainHandler(webapp.RequestHandler):
    
    _tinymce = """
        <!--<script language="javascript" type="text/javascript" src="/scripts/tiny_mce/tiny_mce.js"></script>-->
    """
    
    _analytics = """
            <script type="text/javascript">
                var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
                document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
            </script>
            <script type="text/javascript">
                var pageTracker = _gat._getTracker("UA-3233899-16");
                pageTracker._trackPageview();
            </script> 
    """
    
    def get(self):
        user = users.get_current_user()
        login_url = users.create_login_url("")
        logout_url = users.create_logout_url("")
        
        group_admin = ''
        username = ''
        head_scripts = ''
        footer_analytics = MainHandler._analytics
        if user:
            username = user.nickname()
            if (users.is_current_user_admin()):
                group_admin = 'admin'
                head_scripts = '' #MainHandler._tinymce
                footer_analytics = ''

        template_values = { 'username':         username, 
                            'usergroups':       group_admin,
                            'login_url':        login_url, 
                            'logout_url':       logout_url,
                            'head_scripts':     head_scripts,
                            'footer_scripts':   footer_analytics, 
                            };
                            
        logging.info("MainHandler.get: user[%s] admin[%s]" % (username, group_admin))
        
        path=os.path.dirname( os.path.dirname(__file__) )+'/templates/main.html'
        
        try:
            self.response.out.write(template.render( path, template_values))            
            self.response.headers["Content-Type"]  = "text/html; charset=utf-8"
            self.response.headers["Cache-Control"] = "no-cache, must-revalidate"
        except:
            self.response.set_status( 404 )

    
#========================================================================================================

def main():
	application = webapp.WSGIApplication([('/', MainHandler) ], debug=True)
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()        